$(function() {

    //var log = console.log;

    //for Caching
    var $content = $('#content');

    /*----------------------------------------------------------------------*/
    /* datatable plugin
    /*----------------------------------------------------------------------*/

    $content.find("table.datatable").dataTable({
        "sPaginationType": "two_button",
        "oLanguage": {
            "sLengthMenu": "Prikaži _MENU_ medija po strani",
            "sZeroRecords": "Nema podataka - !",
            "sInfo": "Prikazujem _START_ - _END_ od _TOTAL_ medija",
            "sInfoEmpty": "Prikazujem 0 do 0 od 0 članaka",
            "sInfoFiltered": "(filtritam od _MAX_ ukupnih medija)"
        },
        "aoColumnDefs": [ 
        { "bSortable": false, "aTargets": [0,null,2,null,null,null,null,7] }
        ],
        "iDisplayLength" : "25",
        "aaSorting": [[ 4, "desc" ]] 
    });

    $('.deletemedia').live('click',function(){

        var conf = confirm("Da li ste sigurni?")
        if (!conf){
            return false;
        }

        loader();

        var id = this.id

        $.ajax({
            type: "POST",
            url: base_url+'media/deleteone',
            data: {
                id: id
            },
            dataType: 'json',
            success: function(data){

                if (data.action == true){

                    //log('successful action')
                    $('#'+id).parent().parent().fadeOut(900,function(){
                        $(this).remove();
                    });

                    $('.dataTables_info').fadeOut('300');

                    $.modal.close(); 

                }else if (data.action == false){

                    alert(data.msg)

                    $.modal.close();

                }else alert("Error on server:"+data.msg);


            }
        })

        return false;

    })

    /***********************************************************
    * LOADERS
    ***********************************************************/

    function loader()
    {
        $.modal(
        "<div><div class='animate'><img src='"+base_url+"assets/img/backgnds/loading.gif'></div><h2>Molim sačekajte...</h2></div>" , {
            closeHTML: "",
            containerCss:{
                height:70,
                width:120,
                borderColor:"#dedede"
            },
            opacity:20,
            overlayCss: {
                backgroundColor: "#000000"
            }
        });
    }

    $('#directory').live('change', function(){

        if($(this).val()==0) return false;

        loader();

        var id = $(this).val();

        $.ajax({
            type: "POST",
            url: base_url+'media/getMediaByFolder',
            data: {
                id: id,
                who: 'media'
            },
            dataType: 'json',
            success: function(data){

                if (data.action == true){

                    //log('successful action')

                    $('.ex_highlight_row').html('');                 

                    $('.ex_highlight_row').append(data.html);

                    /* $.each(data.jsonData, function(key, value) { 

                    var html='';
                    html += '<tr>';


                    var created = new Date(value['created']*1000);
                    var modified = new Date(value['modified']*1000);

                    html += '<td class="naziv">'+value['name']+'</td>';
                    html += '<td align="left" height="23"><div style="line-height: 0; font-size: 0"><img src="'+base_url+'tempatt/'+value['file']+'" height="23" /></div></td>';
                    html += '<td class="autor">'+value['author']+'</td>';
                    html += '<td class="autor">'+created.getDate()+'.'+(parseInt(created.getMonth())+1)+'.'+created.getFullYear()+' '+created.getHours()+':'+created.getMinutes()+' h</td>';
                    html += '<td class="autor">'+modified.getDate()+'.'+(parseInt(modified.getMonth())+1)+'.'+modified.getFullYear()+' '+modified.getHours()+':'+modified.getMinutes()+' h</td>';
                    html += '<td class="autor">'+value['tags']+'</td>';
                    html += '<td class="akcija">';
                    html += '<a href="'+base_url+'media/mediapreview?id='+value['id']+'" class="small" id="'+value['id']+'">Pogledaj</a> |';
                    html += '<a href="'+base_url+'media/editform?id='+value['id']+'" class="small" id="'+value['id']+'">Uredi</a> |';
                    html += '<a href="javascript:" class="deletemedia small" id="'+value['id']+'">Obriši</a>';
                    html += '</td>';

                    html += '</tr>'; 

                    $('#example tbody').append(html);

                    });*/ 

                    /*----------------------------------------------------------------------*/
                    /* datatable plugin
                    /*----------------------------------------------------------------------*/

                    $content.find("table.datatable").dataTable({
                        "sPaginationType": "two_button",
                        "oLanguage": {
                            "sLengthMenu": "Prikaži _MENU_ medija po strani",
                            "sZeroRecords": "Nema podataka - !",
                            "sInfo": "Prikazujem _START_ - _END_ od _TOTAL_ medija",
                            "sInfoEmpty": "Prikazujem 0 do 0 od 0 medija",
                            "sInfoFiltered": "(filtritam od _MAX_ ukupnih medija)"
                        },
                        "aoColumnDefs": [ 
                        { "bSortable": false, "aTargets": [0,null,2,null,null,null,null,7] }
                        ],
                        "iDisplayLength" : "25",
                        "aaSorting": [[ 4, "desc" ]] 
                    });

                    $('<div class="dataTables_dir">'+$('#example_dir').html()+'</div>').insertAfter('#example_length');

                    $.modal.close(); 

                }else if (data.action == false){

                    alert(data.msg)

                    $('#directory').val(0);

                    $.modal.close();

                }else alert("Error on server:"+data.msg);


            }
        })

        return false;

    })

    $('#directory').val(0); 
    $('<div class="dataTables_dir">'+$('#example_dir').html()+'</div>').insertAfter('#example_length');

});